<?

class photos {



// FUNCTION // Get photos for tree item
function get_photos($tree_id, $cache = false){

	if(!empty($tree_id)){

		$sql = "SELECT photo_id, tree_id, photo_url_full, photo_url_thumb
		FROM photos
		WHERE tree_id = '".$tree_id."'
		ORDER BY photo_id
		LIMIT 10 "; 
		
		return apply_query($sql, $num_rows, $cache['cache']);
	
	}

}


// FUNCTION // insert photo details
function insert_photo($values){
        
	$values = array_map('array_trim', $values);
	
	$sql = "INSERT INTO photos
	SET tree_id = '".$values['tree_id']."',
	photo_url_full = '".$values['photo_url_full']."',
	photo_url_thumb = '".$values['photo_url_thumb']."' ";

	$result = mysql_query($sql) or die(mysql_error());

}





// FUNCTION // Update a photo
function update_photo($params){

	if(!empty($params)){
        
		$params = array_map('array_trim', $params);
	
		$sql = "UPDATE photos
		SET photo_url_full = '".$params['photo_url_full']."',
		photo_url_thumb = '".$params['photo_url_thumb']."'
		WHERE photo_id = '".$params['photo_id']."' 
		AND tree_id = '".$params['tree_id']."' "; 
			
		$result = mysql_query($sql) or die(mysql_error());
	
	}

}



// FUNCTION // Delete a photo
function delete_photo($params){

	if(!empty($params)){
	
		$sql = "DELETE FROM photos
		WHERE tree_id = '".$params['tree_id']."'
		AND photo_id = '".$params['photo_id']."'
		LIMIT 1 ";
	
		$result = mysql_query($sql) or die(mysql_error());
	
	}

}


// FUNCTION // Delete all photos
function delete_photos($tree_id){
	
	if(!empty($tree_id)){

		$sql = "DELETE FROM photos
		WHERE tree_id = '".$tree_id."' "; 
	
		$result = mysql_query($sql) or die(mysql_error());

	}

}


// FUNCTION // Update a photo
function move_update_photo($params){

	if(!empty($params)){
        
		$params = array_map('array_trim', $params);
	
		$sql = "UPDATE photos
		SET tree_id = '".$params['move_to_tree_id']."'
		WHERE tree_id = '".$params['delete_tree_id']."' "; 
			
		$result = mysql_query($sql) or die(mysql_error());
	
	}

}





function upload_form_photo($photos, $tree_id){
	
	if(!empty($photos)){

		for($i=0; $i<count($photos); $i++){
		
			// Move and insert photos
			if($photos[$i]['photo_url_full']){
			
				$photo_insert = array();
				$photo_insert['tree_id'] = $tree_id;
				$photo_insert['photo_url_full'] = $photos[$i]['photo_url_full'];
				$photo_insert['photo_url_thumb'] = $photos[$i]['photo_url_thumb'];
			
				$this->insert_photo($photo_insert);
			
				// Move images from temp to perminant location
				rename(UPLOAD_ABS_TEMP.$photos[$i]['photo_url_full'], UPLOAD_ABS_PERM.$photos[$i]['photo_url_full']);
				rename(UPLOAD_ABS_TEMP.$photos[$i]['photo_url_thumb'], UPLOAD_ABS_PERM.$photos[$i]['photo_url_thumb']);
			
			}
			
		}
		
	}else{
		
		$this->delete_photos($tree_id);
		
	}

}



}

?>